﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Lucene.Linq.Mapping;
using Lucene.Linq.Search;

namespace Lucene.Linq.VSTests.DatabaseIntegration.LinqToSql
{
    [Document]
    [Table(Name = "dbo.Orders")]
    public partial class Order : IIndexable, INotifyPropertyChanging, INotifyPropertyChanged
    {
        #region Fields

        private static readonly PropertyChangingEventArgs emptyChangingEventArgs =
            new PropertyChangingEventArgs(String.Empty);

        private EntityRef<Customer> _Customer;

        private string _CustomerID;
        private int? _EmployeeID;
        private decimal? _Freight;
        private DateTime? _OrderDate;
        private int _OrderID;
        private DateTime? _RequiredDate;
        private string _ShipAddress;
        private string _ShipCity;
        private string _ShipCountry;
        private string _ShipName;
        private string _ShipPostalCode;
        private string _ShipRegion;
        private int? _ShipVia;
        private DateTime? _ShippedDate;

        #endregion

        #region Extensibility Method Definitions

        partial void OnLoaded();
        partial void OnValidate();
        partial void OnCreated();
        partial void OnOrderIDChanging(int value);
        partial void OnOrderIDChanged();
        partial void OnCustomerIDChanging(string value);
        partial void OnCustomerIDChanged();
        partial void OnEmployeeIDChanging(int? value);
        partial void OnEmployeeIDChanged();
        partial void OnOrderDateChanging(DateTime? value);
        partial void OnOrderDateChanged();
        partial void OnRequiredDateChanging(DateTime? value);
        partial void OnRequiredDateChanged();
        partial void OnShippedDateChanging(DateTime? value);
        partial void OnShippedDateChanged();
        partial void OnShipViaChanging(int? value);
        partial void OnShipViaChanged();
        partial void OnFreightChanging(decimal? value);
        partial void OnFreightChanged();
        partial void OnShipNameChanging(string value);
        partial void OnShipNameChanged();
        partial void OnShipAddressChanging(string value);
        partial void OnShipAddressChanged();
        partial void OnShipCityChanging(string value);
        partial void OnShipCityChanged();
        partial void OnShipRegionChanging(string value);
        partial void OnShipRegionChanged();
        partial void OnShipPostalCodeChanging(string value);
        partial void OnShipPostalCodeChanged();
        partial void OnShipCountryChanging(string value);
        partial void OnShipCountryChanged();

        #endregion

        #region Constructors

        public Order()
        {
            OnCreated();
            _Customer = default(EntityRef<Customer>);
        }

        #endregion

        #region Properties

        [Field(FieldIndex.Tokenized, FieldStore.Yes)]
        [Column(Storage = "_OrderID", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY",
            IsPrimaryKey = true, IsDbGenerated = true)]
        public int OrderID
        {
            get { return _OrderID; }
            set
            {
                if ((_OrderID != value))
                {
                    OnOrderIDChanging(value);
                    SendPropertyChanging();
                    _OrderID = value;
                    SendPropertyChanged("OrderID");
                    OnOrderIDChanged();
                }
            }
        }

        [Field(FieldIndex.Tokenized, FieldStore.Yes)]
        [Column(Storage = "_CustomerID", DbType = "NChar(5)")]
        public string CustomerID
        {
            get { return _CustomerID; }
            set
            {
                if ((_CustomerID != value))
                {
                    if (_Customer.HasLoadedOrAssignedValue)
                    {
                        throw new ForeignKeyReferenceAlreadyHasValueException();
                    }
                    OnCustomerIDChanging(value);
                    SendPropertyChanging();
                    _CustomerID = value;
                    SendPropertyChanged("CustomerID");
                    OnCustomerIDChanged();
                }
            }
        }

        [Column(Storage = "_EmployeeID", DbType = "Int")]
        public int? EmployeeID
        {
            get { return _EmployeeID; }
            set
            {
                if ((_EmployeeID != value))
                {
                    OnEmployeeIDChanging(value);
                    SendPropertyChanging();
                    _EmployeeID = value;
                    SendPropertyChanged("EmployeeID");
                    OnEmployeeIDChanged();
                }
            }
        }

        [Column(Storage = "_OrderDate", DbType = "DateTime")]
        public DateTime? OrderDate
        {
            get { return _OrderDate; }
            set
            {
                if ((_OrderDate != value))
                {
                    OnOrderDateChanging(value);
                    SendPropertyChanging();
                    _OrderDate = value;
                    SendPropertyChanged("OrderDate");
                    OnOrderDateChanged();
                }
            }
        }

        [Column(Storage = "_RequiredDate", DbType = "DateTime")]
        public DateTime? RequiredDate
        {
            get { return _RequiredDate; }
            set
            {
                if ((_RequiredDate != value))
                {
                    OnRequiredDateChanging(value);
                    SendPropertyChanging();
                    _RequiredDate = value;
                    SendPropertyChanged("RequiredDate");
                    OnRequiredDateChanged();
                }
            }
        }

        [Column(Storage = "_ShippedDate", DbType = "DateTime")]
        public DateTime? ShippedDate
        {
            get { return _ShippedDate; }
            set
            {
                if ((_ShippedDate != value))
                {
                    OnShippedDateChanging(value);
                    SendPropertyChanging();
                    _ShippedDate = value;
                    SendPropertyChanged("ShippedDate");
                    OnShippedDateChanged();
                }
            }
        }

        [Column(Storage = "_ShipVia", DbType = "Int")]
        public int? ShipVia
        {
            get { return _ShipVia; }
            set
            {
                if ((_ShipVia != value))
                {
                    OnShipViaChanging(value);
                    SendPropertyChanging();
                    _ShipVia = value;
                    SendPropertyChanged("ShipVia");
                    OnShipViaChanged();
                }
            }
        }

        [Column(Storage = "_Freight", DbType = "Money")]
        public decimal? Freight
        {
            get { return _Freight; }
            set
            {
                if ((_Freight != value))
                {
                    OnFreightChanging(value);
                    SendPropertyChanging();
                    _Freight = value;
                    SendPropertyChanged("Freight");
                    OnFreightChanged();
                }
            }
        }

        [Field(FieldIndex.Tokenized, FieldStore.Yes, IsDefault = true)]
        [Column(Storage = "_ShipName", DbType = "NVarChar(40)")]
        public string ShipName
        {
            get { return _ShipName; }
            set
            {
                if ((_ShipName != value))
                {
                    OnShipNameChanging(value);
                    SendPropertyChanging();
                    _ShipName = value;
                    SendPropertyChanged("ShipName");
                    OnShipNameChanged();
                }
            }
        }

        [Field(FieldIndex.Tokenized, FieldStore.Yes)]
        [Column(Storage = "_ShipAddress", DbType = "NVarChar(60)")]
        public string ShipAddress
        {
            get { return _ShipAddress; }
            set
            {
                if ((_ShipAddress != value))
                {
                    OnShipAddressChanging(value);
                    SendPropertyChanging();
                    _ShipAddress = value;
                    SendPropertyChanged("ShipAddress");
                    OnShipAddressChanged();
                }
            }
        }

        [Field(FieldIndex.Tokenized, FieldStore.Yes)]
        [Column(Storage = "_ShipCity", DbType = "NVarChar(15)")]
        public string ShipCity
        {
            get { return _ShipCity; }
            set
            {
                if ((_ShipCity != value))
                {
                    OnShipCityChanging(value);
                    SendPropertyChanging();
                    _ShipCity = value;
                    SendPropertyChanged("ShipCity");
                    OnShipCityChanged();
                }
            }
        }

        [Column(Storage = "_ShipRegion", DbType = "NVarChar(15)")]
        public string ShipRegion
        {
            get { return _ShipRegion; }
            set
            {
                if ((_ShipRegion != value))
                {
                    OnShipRegionChanging(value);
                    SendPropertyChanging();
                    _ShipRegion = value;
                    SendPropertyChanged("ShipRegion");
                    OnShipRegionChanged();
                }
            }
        }

        [Column(Storage = "_ShipPostalCode", DbType = "NVarChar(10)")]
        public string ShipPostalCode
        {
            get { return _ShipPostalCode; }
            set
            {
                if ((_ShipPostalCode != value))
                {
                    OnShipPostalCodeChanging(value);
                    SendPropertyChanging();
                    _ShipPostalCode = value;
                    SendPropertyChanged("ShipPostalCode");
                    OnShipPostalCodeChanged();
                }
            }
        }

        [Field(FieldIndex.Tokenized, FieldStore.Yes)]
        [Column(Storage = "_ShipCountry", DbType = "NVarChar(15)")]
        public string ShipCountry
        {
            get { return _ShipCountry; }
            set
            {
                if ((_ShipCountry != value))
                {
                    OnShipCountryChanging(value);
                    SendPropertyChanging();
                    _ShipCountry = value;
                    SendPropertyChanged("ShipCountry");
                    OnShipCountryChanged();
                }
            }
        }

        //[Association(Name = "Customer_Order", Storage = "_Customer", ThisKey = "CustomerID", IsForeignKey = true)]
        //public Customer Customer
        //{
        //  get
        //  {
        //    return this._Customer.Entity;
        //  }
        //  set
        //  {
        //    Customer previousValue = this._Customer.Entity;
        //    if (((previousValue != value)
        //          || (this._Customer.HasLoadedOrAssignedValue == false)))
        //    {
        //      this.SendPropertyChanging();
        //      if ((previousValue != null))
        //      {
        //        this._Customer.Entity = null;
        //        previousValue.Orders.Remove(this);
        //      }
        //      this._Customer.Entity = value;
        //      if ((value != null))
        //      {
        //        value.Orders.Add(this);
        //        this._CustomerID = value.CustomerID;
        //      }
        //      else
        //      {
        //        this._CustomerID = default(string);
        //      }
        //      this.SendPropertyChanged("Customer");
        //    }
        //  }
        //}

        #endregion

        #region Methods

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        #endregion

        protected virtual void SendPropertyChanging()
        {
            if ((PropertyChanging != null))
            {
                PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((PropertyChanged != null))
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
